ソフトウェア/rails/teaspoon + jasmine による javascript のテスト
rails 開発で javascript もテストしたい†
konacha というのもあるそうなのだけれど、chai.js が IE8 に対応していないので、 teaspoon を試してみました。
基本的にこちらをなぞったのみです。
http://kazu69.net/blog/tec/1787
気付いたところのみ書きます。
とりあえず動かす†
Gemfile
group :development, :test do
gem "teaspoon"
end
$ bundle install
$ rails generate teaspoon:install
create config/initializers/teaspoon.rb
create spec/teaspoon_env.rb
create spec/javascripts/support
create spec/javascripts/fixtures
create spec/javascripts/spec_helper.js
+============================================================================+
Congratulations! Teaspoon was successfully installed. Documentation and more can
be found at: https://github.com/modeset/teaspoon
$ rails s
spec/javascript/spec_helper.js で //= require の設定をする。
テストを、spec/javascript/*_spec.coffee に書く
書き方は、http://jasmine.github.io/1.3/introduction.html を見ながら。
基本はこんな。
describe "MyObject", ->
it "does something", ->
obj = new MyObject()
expect( obj.do_something() ).toBe expectation
it "writes something", ->
obj = new MyObject()
expect( obj.write_something() ).toMatch /expectation/
http://127.0.0.1/3000/teaspoon へアクセスすると実行結果やエラーメッセージを
非常に簡単にできました。
konacha とは違って web インターフェースからテストのソースを見ることはできないみたい?
istanbul を使ってカバレッジ測定をする†
$ npm install -g istanbul
/usr/local/bin/istanbul -> /usr/local/lib/node_modules/istanbul/lib/cli.js
istanbul@0.2.10 /usr/local/lib/node_modules/istanbul
├── which@1.0.5
├── abbrev@1.0.5
├── nopt@2.2.1
├── wordwrap@0.0.2
├── async@0.8.0
├── resolve@0.6.3
├── esprima@1.2.2
├── mkdirp@0.5.0 (minimist@0.0.8)
├── fileset@0.1.5 (minimatch@0.3.0, glob@3.2.11)
├── js-yaml@3.0.2 (esprima@1.0.4, argparse@0.1.15)
├── handlebars@1.3.0 (optimist@0.3.7, uglify-js@2.3.6)
└── escodegen@1.3.2 (estraverse@1.5.0, esutils@1.0.0, esprima@1.1.1, source-map@0.1.33)
$ jed spec/teaspoon_env.rb
config.coverage do |coverage|
coverage.reports = ["text-summary", "html"]
end
$ bundle exec teaspoon --coverage=default
Could not find gem 'phantomjs-gem (>= 0) ruby' in the gems available on this machine.
phantomjs を入れる
$ wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-i686.tar.bz2
$ tar fxj phantomjs-1.9.7-linux-i686.tar.bz2
$ cd phantomjs-1.9.7-linux-i686/
$ ls
ChangeLog LICENSE.BSD README.md bin examples third-party.txt
$ ls bin
phantomjs
$ sudo cp bin/phantomjs /usr/local/bin/
$ which phantomjs
/usr/local/bin/phantomjs
$ phantomjs --version
1.9.7
$ bundle exec teaspoon --coverage=default
Starting the Teaspoon server...
Teaspoon running default suite at http://127.0.0.1:3500/teaspoon/default
..........
Finished in 0.01300 seconds
10 examples, 0 failures
=============================== Coverage summary ===============================
Statements : 31.82% ( 7/22 )
Branches : 12.5% ( 2/16 )
Functions : 33.33% ( 2/6 )
Lines : 31.82% ( 7/22 )
================================================================================
なんか出た。